require "hpricot"
require 'rubygems'
require 'uri'
require 'open-uri'

class TermsController < ApplicationController
  # GET /terms
  # GET /terms.xml
  def index
    @terms = Term.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @terms }
    end
  end

  # GET /terms/1
  # GET /terms/1.xml
  def show
    @term = Term.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @term }
    end
  end

  # GET /terms/new
  # GET /terms/new.xml
  def new
    @term = Term.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @term }
    end
  end

  # GET /terms/1/edit
  def edit
    @term = Term.find(params[:id])
  end

  # POST /terms
  # POST /terms.xml
  def create
    @term = Term.new(params[:term])

    respond_to do |format|
      if @term.save
        flash[:notice] = 'Term was successfully created.'
        format.html { redirect_to(@term) }
        format.xml  { render :xml => @term, :status => :created, :location => @term }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @term.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /terms/1
  # PUT /terms/1.xml
  def update
    @term = Term.find(params[:id])

    respond_to do |format|
      if @term.update_attributes(params[:term])
        flash[:notice] = 'Term was successfully updated.'
        format.html { redirect_to(@term) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @term.errors, :status => :unprocessable_entity }
      end
    end
  end

  # GET /terms/1/sync
  def sync
    @term = Term.find(params[:id])

    termName = @term.name[0..1].upcase
    if not (@term.name.upcase =~ /^(SPRING|FALL|SUMMER) (\d{4,4})$/)
      @message = 'Name of each term must be of the form: Spring 20xx or Fall 20xx'
      return
    end

    @departments = DepartmentAbbreviation.list_of_departments
  end

  def sync_department
    @term = Term.find(params[:id])
    termName = @term.name.split(/\s+/)[0].upcase
    if termName == 'SPRING'
      termName = 'SP' 
    elsif termName == 'FALL'
      termName = 'FL'
    else
      render :text => {
        :message => 'fatal error. invalid term'        
      }
    end
    department = params[:department]
    uri = params[:uri]

    if params['debug']:
      @term.verboseParsing = true
    end

    if not uri or not uri.match(/^http:\/\/osoc\.berkeley\.edu\/OSOC\/osoc\?/)
      uri = URI.escape('http://osoc.berkeley.edu/OSOC/osoc?p_term='+termName+'&p_deptname='+department)
    end

    r = @term.fetch_page_and_sync(uri)
    # clean up URI
    @message = 'error' if r[:status] != :success
    next_uri = r[:uri]
    next_uri = next_uri.gsub(/\w+\=\&/,'') if next_uri

    render :text => {
      :next_uri => next_uri,
      :message => @message
    }.to_json
  end

  # DELETE /terms/1
  # DELETE /terms/1.xml
  def destroy
    @term = Term.find(params[:id])
    @term.destroy

    respond_to do |format|
      format.html { redirect_to(terms_url) }
      format.xml  { head :ok }
    end
  end
end
